//
//  XHTrafficUseDetailViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/18.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHTrafficUseDetailViewController.h"

#import "XHTrafficStateCell.h"

#import "Services.h"

@interface XHTrafficUseDetailViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray * useInfoArray;
@end

@implementation XHTrafficUseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHTrafficStateCell" bundle:nil] forCellReuseIdentifier:XHTrafficStateCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self orderSnUseFinancialBenefitsDetail];
}

- (void)orderSnUseFinancialBenefitsDetail
{
    [ServiceManager OrderSnUseFinancialBenefitsWithOrderSn:self.orderSn success:^(NSDictionary *data) {
        self.useInfoArray = (NSArray*)data;
        [self.ui_tableView reloadData];
    } failure:^(NSDictionary *data) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = 1;
    
    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=self.useInfoArray.count;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [self trafficStateCell:indexPath];
    return cell;
}
- (XHTrafficStateCell*)trafficStateCell:(NSIndexPath*)indexPath
{
    XHTrafficStateCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHTrafficStateCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary * dic = self.useInfoArray[indexPath.row];
    if ([dic[@"operateType"]isEqualToString:@"USE"])
    {
        NSString * unit = @"";
        NSInteger values = [dic[@"productValue"] integerValue];
        cell.ui_time.text = [[NSDate dateWithString:dic[@"operateTime"] ]stringDateYear];
        if (values>1000)
        {
            unit = @"G";
            values = values/1000;
        }
        else if (values<1000)
        {
            unit = @"M";
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(![NSString checkNullString:dic[@"phone"]])
        {
            cell.ui_phone.text =[NSString stringWithFormat:@"%@ %@ %@",[dic[@"phone"] substringToIndex:3],
                                 [dic[@"phone"] substringWithRange:NSMakeRange(3,4)],
                                 [dic[@"phone"] substringFromIndex:7]];
        }
        else
        {
            cell.ui_phone.text = @"";
        }
        if ([dic[@"operateStatus"] isEqualToString:@"CALL_SUCCESS"])
        {
            cell.ui_static.text = @"成功";
            cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
        }
        else if ([dic[@"operateStatus"] isEqualToString:@"WAIT_FOR_CALL"])
        {
            cell.ui_static.text = @"等待回调";
            cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
        }
        else if([dic[@"operateStatus"] isEqualToString:@"CALL_FAIL"])
        {
            cell.ui_static.text = @"失败";
            cell.ui_static.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.8];
        }
        if ([dic[@"operatorCode"]isEqualToString:@"10086"])
        {
            cell.ui_number.text = [NSString stringWithFormat:@"中国移动%ld%@ 编号:%@",values,unit,dic[@"apiCallRecoredSEQ"]];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10010"])
        {
            cell.ui_number.text = [NSString stringWithFormat:@"中国联通%ld%@ 编号:%@",values,unit,dic[@"apiCallRecoredSEQ"]];
        }
        else if ([dic[@"operatorCode"]isEqualToString:@"10000"])
        {
            cell.ui_number.text = [NSString stringWithFormat:@"中国电信%ld%@ 编号:%@",values,unit,dic[@"apiCallRecoredSEQ"]];
        }
    }
    else if ([dic[@"operateType"]isEqualToString:@"ATONEFOR"])
    {
        cell.ui_phone.text = [NSString stringWithFormat:@"￥%@",dic[@"principal"]];
        cell.ui_number.text = @"回购总额";
        cell.ui_time.text = [[NSDate dateWithString:dic[@"operateTime"] ]stringDateYear];
        if ([dic[@"operateStatus"] isEqualToString:@"SUCCESS"])
        {
            cell.ui_static.text = @"回购成功";
            cell.ui_static.textColor = [ColorManager colorWithHexString:@"#E45252"];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=60;
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
